package cn.tiancaifan.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.tiancaifan.dao.UserDaoI;
import cn.tiancaifan.dao.impl.UserDaoImpl;
import cn.tiancaifan.entity.SystemResult;
import cn.tiancaifan.entity.User;
import cn.tiancaifan.service.UserServiceI;
import cn.tiancaifan.utils.RandomNameUtil;

import java.sql.Time;
import java.util.List;

public class UserServiceImpl implements UserServiceI {

    private UserDaoI dao = new UserDaoImpl();
    private Digester md5 = new Digester(DigestAlgorithm.MD5);

    @Override
    public SystemResult login(String account, String password) {
        if (StrUtil.isBlankIfStr(account) || StrUtil.isBlankIfStr(password)){
            return SystemResult.error("账号密码不能为空");
        }
        User user = dao.findUserByAccount(account);
        if (user == null){
            return SystemResult.error("账号不存在！");
        }
        if (!user.getPassword().equals(md5.digestHex(password))){
            return SystemResult.error("密码错误！");
        }
        user.setPassword("");
        return SystemResult.ok("登陆成功！",user);
    }

    @Override
    public SystemResult register(String account, String password) {
        if (StrUtil.isBlankIfStr(account) || StrUtil.isBlankIfStr(password)){
            return SystemResult.error("账号密码不能为空");
        }
        User findUser = dao.findUserByAccount(account);
        if (findUser != null){
            return SystemResult.error("账号已经存在！");
        }
        User user = new User();
        String passwordHex = md5.digestHex(password);
        user.setAccount(account);
        user.setPassword(passwordHex);
        user.setNikename(RandomNameUtil.getRandomJianHan(5));
        user.setRole(1);
        int c = RandomUtil.randomInt(1, 5);
        user.setAvatar("/avatar/"+c+".jpeg");
        user.setCreateId(0);
        user.setUpdateId(0);
        user.setCreateTime(String.valueOf(System.currentTimeMillis()));
        user.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        int i = dao.insertUser(user);
        if (i != 1 ){
            return SystemResult.error("出现未知错误！");
        }
        user.setPassword(null);
        return SystemResult.ok("注册成功！",user);
    }

    @Override
    public SystemResult findAllUser(long id) {
        List<User> allUser = dao.findAllUser(id);
        return SystemResult.ok("查询成功！",allUser);
    }

    @Override
    public SystemResult findUserBycondition(User parameters, long id) {
        List<User> allUser = dao.findUserBycondition(parameters,id);
        return SystemResult.ok("查询成功！",allUser);
    }

    @Override
    public SystemResult deleteUserByid(String[] ids) {
        int i = dao.deleteUserByid(ids);
        if (i==0)
           return SystemResult.error("删除失败！");
        return SystemResult.ok("删除成功！",null);
    }

    @Override
    public SystemResult updateUserById(User user) {
        if (user.getNikename() == null){
            return SystemResult.error("修改错误！code:1");
        }
        if (user.getRole() == 0){
            return SystemResult.error("修改错误！code:3");
        }
        if(user.getAddress()==null){
            user.setAddress("");
        }
        if (user.getAvatar() == null)
            user.setAvatar("");
        if (user.getTel() == null)
            user.setTel("");
        user.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        int i = dao.updateUserById(user);
        if (i == 0)
            return SystemResult.error("未知错误");
        return SystemResult.ok("修改成功 ！",null);
    }
}
